package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.dao.TPlanMapper;
import com.example.model.entity.TEquipment;
import com.example.model.entity.TPlan;
import com.example.model.query.TEquipmentQuery;
import com.example.model.vo.AreaTree;
import com.example.model.vo.TEquipmentPartVo;
import com.example.service.TEquipmentPartService;
import com.example.service.TEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.model.entity.TEquipmentPart;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * 告警设备通道表
 * @author zhangr
 * @Description Created on 2020-12-16
 */

@RestController
@Controller
@RequestMapping("/tEquipmentPart")
@CrossOrigin
@Api(value = "告警设备通道表管理",tags={"告警设备通道表接口"})
public class TEquipmentPartController {

    @Autowired
    private TEquipmentService equipmentService;

    @Autowired
    private TEquipmentPartService equipmentPartService;

    @Autowired
    private TPlanMapper planMapper;

    /**
     * 获取所有设备
     * @return
     */
    @ApiOperation(value = "获取某设备所有通道", notes = "获取某设备所有通道", httpMethod = "GET")
    @GetMapping("/getList/{id}")
    public ApiResult<List<TEquipmentPartVo>> getList(@PathVariable("id") String id){
        try {
            if(StringUtils.isBlank(id)){
                return new ApiResult<>(-1,"设备id不能为空");
            }
            QueryWrapper<TEquipmentPart> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TEquipmentPart::getIsValid,1L)
                    .eq(TEquipmentPart::getEquipmentId,id).orderByAsc(TEquipmentPart::getPartNum);
            List<TEquipmentPart> list = equipmentPartService.list(queryWrapper);
            List<TEquipmentPartVo> listVo = new ArrayList<>();
            for(TEquipmentPart part : list){
                TEquipmentPartVo vo = new TEquipmentPartVo();
                BeanUtils.copyProperties(part,vo);
                //查是否绑定摄像头
                List<TPlan> planList = planMapper.getPlanByPartId(part.getPartId());
                String videoId = "";
                for(TPlan plan : planList){
                    if(plan.getPlanType() == 3L){
                        videoId = videoId +plan.getVideoId()+",";
                    }
                }
                vo.setVideoId(videoId);
                listVo.add(vo);
            }
            return new ApiResult<>(listVo);
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_CUSTOMER_FALSE);
        }
    }


    /**
     * 模拟报警
     * @return
     */
    @ApiOperation(value = "模拟报警", notes = "模拟报警", httpMethod = "GET")
    @GetMapping("/fakerWarning/{id}/{status}")
    public ApiResult<Boolean> fakerWarning(@PathVariable("id") String id,@PathVariable("status") Long status){
        try {
            TEquipmentPart part = equipmentPartService.getById(id);
            if(part == null || part.getIsValid()==0L){
                return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_EXPORT_NULL);
            }
            return equipmentService.dealMessage(id,status,part);
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_EXPORT_FALSE);
        }
    }
}

